package com.om.abom.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

public class ProcessManagement
{
    public static final String CMD_GET_PID = "ps -ef |grep -v grep |grep abom |awk '{print$2}'";
    public static final String CMD_START = "(java -jar /root/abom-0.0.1-SNAPSHOT.jar &> /tmp/abom.log &)";
    public static final int WAIT_TIME_OF_PROCESS_PREPARATION = 2000;


    public static String processStart(JschExecutor executor) throws Exception
    {
        String pid = executor.execCmd(CMD_GET_PID);
        if (StringUtils.isNotEmpty(pid)) {
            return "服务已经在运行";
        }

        executor.execCmd(CMD_START);
        Thread.sleep(WAIT_TIME_OF_PROCESS_PREPARATION);

        pid = executor.execCmd(CMD_GET_PID);
        if (StringUtils.isEmpty(pid)) {
            return "启动失败";
        }
        return null;
    }

    public static String processStop(JschExecutor executor) throws Exception
    {
        String pid = executor.execCmd(CMD_GET_PID);
        if (StringUtils.isEmpty(pid)) {
            return "服务没有在运行";
        }

        executor.execCmd("kill -9 " + pid);
        Thread.sleep(WAIT_TIME_OF_PROCESS_PREPARATION);

        pid = executor.execCmd(CMD_GET_PID);
        if (StringUtils.isNotEmpty(pid)) {
            return "停止失败";
        }
        return null;
    }
}
